﻿﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

using GraphicsLib;

namespace TowerDefence.UI
{
    class UIManager
    {        
        private VerticalFlowContainer rightClickMenu;
        private SpriteBatch spriteBatch;
        private Graphics2D graphics2D;
        private SpriteFont font;
        private MouseState mouseState;
        private Rectangle windowArea;

        public UIManager(SpriteBatch _spriteBatch, Graphics2D _graphics2D, SpriteFont _font, Rectangle _windowArea)
        {
            this.mouseState = Mouse.GetState();
            this.spriteBatch = _spriteBatch;
            this.graphics2D = _graphics2D;
            this.font = _font;
            this.windowArea = _windowArea;
        }

        public void Load(ContentManager content)
        {
            LoadRightClickMenu(content);          

        }

        private void LoadRightClickMenu(ContentManager content)
        {
            rightClickMenu = new VerticalFlowContainer(new Point(200, 200));
            rightClickMenu.CurrentState().padding = 2;
            rightClickMenu.CurrentState().bgColour = new Color(0, 0, 0, 0.2f);
                        
            var icon = content.Load<Texture2D>("art/menu/test");
           
            String[] str = new String[4];
            str[0] = "I love it when";
            str[1] = "they call me Big Poppa";
            str[2] = "I only smoke blunts";
            str[3] = "if they rolled proper";

            for (int i=0; i<4; ++i)
            {
                HorizontalFlowContainer button = new UI.HorizontalFlowContainer(new Point(0, 0));
                button.CurrentState().bgColour = new Color(0, 0, 0, 0);
                button.CurrentState().padding = 0;
               
                rightClickMenu.AddChild(button);

                Image uiImage = new UI.Image(icon);
                uiImage.CurrentState().rect = new Rectangle(0, 0, 20, 20);                
                uiImage.CurrentState().bgColour = Color.TransparentWhite;
                uiImage.CurrentState().align = (int)(UIElement.eAlign.V_CENTRE | UIElement.eAlign.H_CENTRE); 
                uiImage.CurrentState().padding = 0;

                UIElement.StateDef highlightedImg = uiImage.CurrentState().Copy();
                highlightedImg.imgColour =  new Color(0, 255, 0, 255);                
                uiImage.AddState(highlightedImg, UIElement.eState.HIGHLIGHTED);

                button.AddChild(uiImage);

                EmptySpace es = new EmptySpace();
                es.CurrentState().rect = new Rectangle(0, 0, 10, 0);

                button.AddChild(es);
                
                Text uiText = new UI.Text(str[i], this.font, Color.LightGreen);
                uiText.CurrentState().bgColour = new Color(0, 0, 0, 0.4f);
                uiText.CurrentState().borderColour = Color.Black;
                uiText.CurrentState().padding = 2;
                uiText.CurrentState().borderSize = 1;
                uiText.CurrentState().align = (int)(UIElement.eAlign.V_CENTRE | UIElement.eAlign.LEFT);              

                uiImage.Layout();
                
                // second state 
                UIElement.StateDef uiTextHighlighted = uiText.CurrentState().Copy();
                uiTextHighlighted.bgColour = new Color(255,0,0,0.5f);
                
                uiTextHighlighted.padding = 5;
                uiText.AddState(uiTextHighlighted, UIElement.eState.HIGHLIGHTED);

                // third state 
                uiText.SetState(UIElement.eState.HIGHLIGHTED); // so we can copy highlighted
                UIElement.StateDef uiTextSelected = uiText.CurrentState().Copy();
                uiTextSelected.bgColour = Color.LightGreen;
                uiText.AddState(uiTextSelected, UIElement.eState.SELECTED);
                uiText.SetState(UIElement.eState.NORMAL); // reset to normal

                button.AddChild(uiText);             
           }

            rightClickMenu.bActive = false;
        }

        public void Draw()
        {
            this.rightClickMenu.Draw(this.graphics2D, this.spriteBatch);       
     
            // draw mouse
            graphics2D.DrawRectangle(new Rectangle(mouseState.X - 2, mouseState.Y - 2, 4, 4), Color.Red);
        }

        public void Update()
        {
            mouseState = Mouse.GetState();

            if (mouseState.RightButton == ButtonState.Pressed)
            {
                if (!rightClickMenu.bActive)
                {
                    rightClickMenu.bActive = true;
                    rightClickMenu.curState.rect = new Rectangle(mouseState.X, mouseState.Y, rightClickMenu.curState.rect.Width, rightClickMenu.curState.rect.Height);
                    rightClickMenu.Layout();

                    if (rightClickMenu.curState.rect.X + rightClickMenu.curState.rect.Width > this.windowArea.Width)
                    {
                        rightClickMenu.curState.rect = new Rectangle(rightClickMenu.curState.rect.X - rightClickMenu.curState.rect.Width,
                                                                     rightClickMenu.curState.rect.Y,
                                                                     rightClickMenu.curState.rect.Width,
                                                                     rightClickMenu.curState.rect.Height);

                        rightClickMenu.Layout();
                    }
                    if (rightClickMenu.curState.rect.Y + rightClickMenu.curState.rect.Height > this.windowArea.Height)
                    {
                        rightClickMenu.curState.rect = new Rectangle(rightClickMenu.curState.rect.X,
                                                                     rightClickMenu.curState.rect.Y - rightClickMenu.curState.rect.Height,
                                                                     rightClickMenu.curState.rect.Width,
                                                                     rightClickMenu.curState.rect.Height);

                        rightClickMenu.Layout();
                    }
                }
            }
            else
                rightClickMenu.bActive = false;

            this.rightClickMenu.Update(mouseState);
        }
    }
}